package com.oliver.oliveraiagent.rag;

import com.oliver.oliveraiagent.rag.LoveAppContextualQueryAugmenterFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.advisor.RetrievalAugmentationAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.rag.generation.augmentation.ContextualQueryAugmenter;
import org.springframework.ai.rag.retrieval.search.VectorStoreDocumentRetriever;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.filter.Filter;
import org.springframework.ai.vectorstore.filter.FilterExpressionBuilder;

/**
 * @ClassName LoveAppRagCustomAdvisorcFatory
 * @Description TODO
 * @Author snow
 * @Date 2025/5/22 0:14
 **/

/**
 * 恋爱大师 Advisor 工厂
 */
@Slf4j
public class LoveAppRagCustomAdvisorFactory {

    /**
     * 创建过滤不同元信息状态的Advisor
     * @param vectorStore
     * @param status
     * @return
     */
    public static Advisor createRagCustomAdvisor(VectorStore vectorStore, String status) {
        Filter.Expression expression = new FilterExpressionBuilder()
                .eq("status", status) // 文档的元信息
                .build();

        VectorStoreDocumentRetriever documentRetriever = VectorStoreDocumentRetriever.builder()
                .vectorStore(vectorStore)
                .filterExpression(expression) // 过滤条件
                .similarityThreshold(0.5)   // 相似度阈值
                .topK(3)    //  返回的文档数量
                .build();

        //  创建查询增强顾问
        return RetrievalAugmentationAdvisor.builder()
                .documentRetriever(documentRetriever)
                //  设置是否允许上下文为空
                .queryAugmenter(LoveAppContextualQueryAugmenterFactory.createInstance())
                .build();
    }

}
